package com.weixin.paleituo.broadcast;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.weixin.paleituo.activity.BuyHouseStepActivity;
import com.weixin.paleituo.activity.CouponsActivity;
import com.weixin.paleituo.activity.FlashActivity;
import com.weixin.paleituo.activity.MyMessageActivity;
import com.weixin.paleituo.activity.OtherLoginDialogActivity;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.eventbus.ExitLoginSuccess;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Author yichao
 * Time  2018/2/12 14:24
 * Dest  接受通知
 */

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3;   //推送通知最多显示条数
    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        //    LogUtils.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtils.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtils.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtils.d(TAG, "接受到推送下来的通知");

            receivingNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtils.d(TAG, "用户点击打开了通知");

            openNotification(context, bundle);

        } else {
            LogUtils.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }
    private void receivingNotification(final Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        LogUtils.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        LogUtils.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtils.d(TAG, "extras : " + extras);
        String content_type = bundle.getString(JPushInterface.EXTRA_ALERT_TYPE);
        LogUtils.d(TAG, "content_type : " + content_type);

        if (content_type.equalsIgnoreCase("5")){

            OtherLoginDialogActivity.myStartActivity(context);




        }

    }
    private void openNotification(Context context, Bundle bundle) {

       /* String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtils.d(TAG, "extras : " + extras);*/
        String content_type = bundle.getString(JPushInterface.EXTRA_ALERT_TYPE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtils.d(TAG, "content_type : " + content_type);
        List<Activity> activities = BaseApplication.getmActivitys();
        if (activities != null && activities.size() > 0 && BaseApplication.isLoginSuccess) {
            //登录有
            LogUtils.d(TAG, "启动activity的个数 ：：：：" + activities.size() + "");
            if (!StringUtils.isEmpty(content_type)) {

                switch (content_type) {
                    case "1":
                        //系统消息
                        Intent intent = new Intent(context, MyMessageActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(Contans.INTENT_EXTRA,BaseApplication.getUserId());
                        context.startActivity(intent);
                        break;
                    case "2":
                        //签约消息
                        String id = "";
                        try {
                            JSONObject extrasJson = new JSONObject(extras);
                            id = extrasJson.optString("id");
                            String userName=extrasJson.optString("username");
                            Intent mIntent = new Intent(context, BuyHouseStepActivity.class);
                            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mIntent.putExtra(Contans.INTENT_DATA, id);
                            mIntent.putExtra(Contans.INTENT_EXTRA,userName);
                            context.startActivity(mIntent);

                        } catch (Exception e) {
                            LogUtils.w(TAG, "Unexpected: extras is not a valid json", e);
                        }
                        break;
                    case "3":
                        //优惠券消息
                       // CouponsActivity.startActivity();
                        Intent mIntent = new Intent(context, CouponsActivity.class);
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(mIntent);
                        break;
                    case "4":
                            //佣金消
                            Intent intent1 = new Intent(context, MyMessageActivity.class);
                            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent1.putExtra(Contans.INTENT_DATA,BaseApplication.getUserId());
                            context.startActivity(intent1);
                            break;
                }
            }
        } else {
            //没登录
            LogUtils.d(TAG, "启动activity的个数 ：：：：为空 0");
            if (!StringUtils.isEmpty(content_type)) {
                    FlashActivity.startActivity(context,content_type,extras);




            }


        }


    }





}